package com.sky.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Date;
import java.util.Map;

public class JwtUtils {
    // JWT的默认过期时间，单位为毫秒，默认为1小时
    public static final long EXPIRATION_TIME = 1000 * 60 * 60*24*2;
    // JWT签名密钥，实际应用中应妥善保管
    public static final String SECRET_KEY = "12dasdasdasdsssssssssssssssss" +
            "ssssssssssssssssssssssssssssssssssssssssss";

    public static String createJwt(Map<String, Object> claims) {
        SecretKey key = Keys.hmacShaKeyFor(SECRET_KEY.getBytes());
        String token = Jwts.builder().setClaims(claims).signWith(key, SignatureAlgorithm.HS256).
                setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME)).compact();
        return  token;
    }
    public static Claims parseJwt(String jwt) {
        Claims body = Jwts.parser().setSigningKey(SECRET_KEY.getBytes())
                .build().parseClaimsJws(jwt).getBody();
        return body;
    }
}
